#include "highScore.h"

highScore::highScore()
{
	data.clear();
}


highScore::~highScore()
{
}

void highScore::read(string filename){
	data.clear();
	in = ifstream(filename);
	string line;
	int i = 0;
	while (getline(in, line)){
		istringstream iss(line);
		int score;
		string player;
		getline(iss, player, ';');
		iss >> score;
		data.push_back(highScoreData(player, score));
		i++;
	}
}

void highScore::write(string filename){
	out = ofstream(filename);
	for (int i = 0; i < data.size(); i++){
		out << data[i].getPlayer() << ";" << data[i].getScore() << endl;
	}
}

bool comp(highScoreData& d1, highScoreData& d2){
	return d1.getScore() > d2.getScore();
}

void highScore::add(string player, int score){
	
	//add new score to end of list
	data.push_back(highScoreData(player, score));

	sort(data.begin(), data.end(), comp);
	if (data.size() > 10){
		data.pop_back();
	}

}